package com.gravata.enigma.activities;

import java.sql.SQLException;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.gravata.enigma.R;
import com.gravata.enigma.model.Filtro;
import com.gravata.enigma.model.Regra;
import com.gravata.enigma.model.Satelite;
import com.gravata.enigma.model.Transponder;

public class FiltroActivity extends Activity {

	private EditText campoRegra;
	private Spinner satelite,transponder;
	private CheckBox negado,exato,inicio,fim,hd;
	private TextView labelExato,labelInicio,labelFim,labelHD,labelSatelite,labelTransponder;
	private Filtro filtro;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.filtro_screen);

		getActionBar().setDisplayHomeAsUpEnabled(true);

		campoRegra=(EditText) findViewById(R.id.regras_regra);
		negado=(CheckBox) findViewById(R.id.regras_negado);
		exato=(CheckBox) findViewById(R.id.regras_exata);
		inicio=(CheckBox) findViewById(R.id.regras_inicio);
		fim=(CheckBox) findViewById(R.id.regras_fim);
		hd=(CheckBox) findViewById(R.id.regras_hd);

		satelite=(Spinner) findViewById(R.id.regras_satelite);
		transponder=(Spinner) findViewById(R.id.regras_transponder);

		labelExato=(TextView) findViewById(R.id.regras_label_exata);
		labelInicio=(TextView) findViewById(R.id.regras_label_inicio);
		labelFim=(TextView) findViewById(R.id.regras_label_fim);
		labelHD=(TextView) findViewById(R.id.regras_label_hd);
		labelSatelite=(TextView) findViewById(R.id.regras_label_satelite);
		labelTransponder=(TextView) findViewById(R.id.regras_label_transponder);


		satelite.setAdapter(TelaInicial.sateliteComboAdapter);
		transponder.setAdapter(TelaInicial.transponderComboAdapter);

		satelite.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> adapter, View arg1,
						int position, long arg3) {

					if(adapter.getAdapter().getItem(position).getClass().isAssignableFrom(Satelite.class)){
						Satelite satelite=(Satelite) adapter.getAdapter().getItem(position);
						TelaInicial.transponderComboAdapter.setData(new ArrayList<Transponder>(satelite.getTransponders()));

					}else{
						TelaInicial.transponderComboAdapter.setData(new ArrayList<Transponder>());
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {

				}
			});

		if(getIntent().getSerializableExtra("filtro")!=null){
			filtro=(Filtro) getIntent().getSerializableExtra("filtro");

			campoRegra.setText(filtro.getFiltro());
			negado.setChecked(filtro.isNegado());
			exato.setChecked(filtro.isCompleta());
			inicio.setChecked(filtro.isInicio());
			fim.setChecked(filtro.isFim());
			hd.setChecked(filtro.isHd());

			if(filtro.getSatelite()!=null)
				satelite.setSelection( TelaInicial.sateliteComboAdapter.getPosition(filtro.getSatelite()));
			if(filtro.getTransponder()!=null)
				transponder.setSelection(TelaInicial.transponderComboAdapter.getPosition(filtro.getTransponder()));

			if(filtro.isCompleta()){
				inicio.setVisibility(View.GONE);
				fim.setVisibility(View.GONE);
				labelInicio.setVisibility(View.GONE);
				labelFim.setVisibility(View.GONE);

			}

			if(filtro.isInicio()){
				exato.setVisibility(View.GONE);
				fim.setVisibility(View.GONE);
				labelExato.setVisibility(View.GONE);
				labelFim.setVisibility(View.GONE);
			}
			if(filtro.isFim()){
				exato.setVisibility(View.GONE);
				labelInicio.setVisibility(View.GONE);
				labelExato.setVisibility(View.GONE);

			}

		}else{
			filtro=new Filtro();
			filtro.setRegra((Regra) getIntent().getSerializableExtra("regra"));
		}


		exato.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					inicio.setChecked(false);
					fim.setChecked(false);
					inicio.setVisibility(View.GONE);
					fim.setVisibility(View.GONE);
					labelInicio.setVisibility(View.GONE);
					labelFim.setVisibility(View.GONE);
				}else{
					inicio.setVisibility(View.VISIBLE);
					fim.setVisibility(View.VISIBLE);
					labelInicio.setVisibility(View.VISIBLE);
					labelFim.setVisibility(View.VISIBLE);
				}
			}
		});

		inicio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					exato.setChecked(false);
					fim.setChecked(false);
					exato.setVisibility(View.GONE);
					fim.setVisibility(View.GONE);
					labelExato.setVisibility(View.GONE);
					labelFim.setVisibility(View.GONE);
				}else{
					exato.setVisibility(View.VISIBLE);
					fim.setVisibility(View.VISIBLE);
					labelExato.setVisibility(View.VISIBLE);
					labelFim.setVisibility(View.VISIBLE);

				}
			}
		});

		fim.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					exato.setChecked(false);
					inicio.setChecked(false);
					exato.setVisibility(View.GONE);
					inicio.setVisibility(View.GONE);
					labelExato.setVisibility(View.GONE);
					labelInicio.setVisibility(View.GONE);
				}else{
					exato.setVisibility(View.VISIBLE);
					inicio.setVisibility(View.VISIBLE);
					labelExato.setVisibility(View.VISIBLE);
					labelInicio.setVisibility(View.VISIBLE);
				}
			}
		});

	}

	private boolean validate(){
		return !campoRegra.getText().toString().isEmpty();
	}

	private void salvar(){
		if(validate()){
			filtro.setFiltro(campoRegra.getText().toString());
			filtro.setNegado(negado.isChecked());
			filtro.setInicio(inicio.isChecked());
			filtro.setFim(fim.isChecked());
			filtro.setCompleta(exato.isChecked());
			filtro.setHd(hd.isChecked());

			Object objS=TelaInicial.sateliteComboAdapter.getItem(satelite.getSelectedItemPosition());
			if(objS.getClass().isAssignableFrom(Satelite.class))
				filtro.setSatelite((Satelite) objS);
			else
				filtro.setSatelite(null);

			Object objT=TelaInicial.transponderComboAdapter.getItem(transponder.getSelectedItemPosition());
			if(objT.getClass().isAssignableFrom(Transponder.class))
				filtro.setTransponder((Transponder) objT);
			else
				filtro.setTransponder(null);

			try {
				TelaInicial.repositorioFiltro.inserirOuAtualizar(filtro);
				setResult(1, getIntent());
				Toast.makeText(this, R.string.salvo_sucesso, Toast.LENGTH_LONG).show();

			} catch (SQLException e) {
				setResult(0, getIntent());
				Toast.makeText(this, R.string.erro_salvar, Toast.LENGTH_LONG).show();

			}

			finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.regra, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_regra_salvar:
			salvar();
			break;
		case R.id.menu_regra_cancelar:

			setResult(0, getIntent());
			finish();

			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;

	}

}
